package com.zhiyou100.javaweb.myservlet.homework.day004.studentmanagementsystem;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @packageName: javase_26
 * @className: ${NAME}
 * @Description: TODO 获取所有的学生，并打印出来
 * @author: yang
 * @date: 2020/5/25
 */
@WebServlet(name = "Day004ServletGetAll", urlPatterns = "/day04/getAll")
//@WebServlet(name = "Day004ServletGetAll",urlPatterns = "day04/getAll")
public class Day004ServletGetAll extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 1 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 2 设定类型
        StudentDao studentDao = new StudentDao();
        // 3 new studentDao 类
        ArrayList<Student> all = studentDao.getAll();
        // 4 获取所有学生
        StringBuilder message = new StringBuilder(" ");
        //  创建一个StringBuilder 用来装信息

        HttpSession session = request.getSession();
        // 获取Session
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        // 获取名为teacher的属性
        String teacherSession = "<h1 align='center'>" + teacher.getTeacherName() + " 老师获取的的所有学生</h1>";
        // 拼接一个信息
        ServletContext servletContext = this.getServletContext();
        Object loginNumber = servletContext.getAttribute("loginNumber");
        String teacherLoginNumber = "<h1 align='center'>当前登陆人数:" + loginNumber + "</h1>";
        message.append("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>获取所有</title>\n" +
                "    <style>\n" +
                "        table td{\n" +
                "            border: 1px solid red;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body style=\"border: 1px #66ccff solid;margin: 10px auto;height: 700px;width: 900px\">\n");
        // 添加表头
        message.append(teacherSession);
        // 添加老师信息
        message.append(teacherLoginNumber);
        // 添加人数信息
        message.append("<table>\n" +
                "    <tr>\n" +
                "        <td>id</td>\n" +
                "        <td>姓名</td>\n" +
                "        <td>性别</td>\n" +
                "        <td>成绩</td>\n" +
                "    </tr>");
        for (Student student : all) {
            Integer studentId = student.getStudentId();
            String studentName = student.getStudentName();
            String studentGender = student.getStudentGender();
            Double studentScore = student.getStudentScore();
            message.append("<tr><td>").append(studentId).append("</td><td>").append(studentName).append("</td> <td>").append(studentGender).append("</td> <td>").append(studentScore).append("</td></tr>");
        }
        message.append("</table>\n" +
                "</body>\n" +
                "</html>");
        PrintWriter out = response.getWriter();
        out.write(message.toString());
        out.flush();

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
